package com.site.service.impl.master;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.site.dao.master.CodeDao;
import com.site.dto.master.CodeBizBean;
import com.site.model.master.CodeInfo;
import com.site.service.master.CodeService;
import com.site.util.PropertyUtils;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

/**
 * 
 * @author Lee 2013-02-25
 *
 */
@Service("codeService")
public class CodeServiceImpl implements CodeService {

	@Autowired
	private CodeDao codeDao;

	@Override
	public Object execute(Object obj, String command) throws SystemException,
			ApplicationException {
		try {
			if (COMMAND_SAVE.equals(command)) {
				saveCode((CodeBizBean) obj);
			} else if (COMMAND_UPDATE.equals(command)) {
				updateCode((CodeBizBean) obj);
			} else if (COMMAND_DELETE.equals(command)) {
				codeDao.markAsDeleted((Integer) obj);
			} else if(COMMAND_DELETE_ALL.equals(command)){
				deleteAll((List<CodeBizBean>)obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SystemException(e);
		}
		return null;
	}

	/**
	 * 删除编码
	 * @param obj
	 */
	private void deleteAll(List<CodeBizBean> obj) {
		for (CodeBizBean codeBizBean : obj) {
			if(null != codeBizBean && null != codeBizBean.getCodeId()){
				codeDao.markAsDeleted(codeBizBean.getCodeId());
			}
		}
	}

	@Override
	public List<CodeBizBean> getAllCodeBizBean() {
		List<CodeBizBean> codeVoList = new ArrayList<CodeBizBean>();
		List<CodeInfo> codePoList = codeDao.getAllCodeInfo();
		if(null != codePoList){
			for (CodeInfo codePo : codePoList) {
				CodeBizBean codeVo = new CodeBizBean();
				try {
					PropertyUtils.copyProperties(codeVo, codePo);
				} catch (Exception e) {
					e.printStackTrace();
					throw new ApplicationException("从po到vo，属性拷贝错误！", e);
				}
				codeVoList.add(codeVo);
			}
		}
		return codeVoList;
	}
	
	/**
	 * 分页获取所有的Code
	 * @return
	 */
	@Override
	public List<CodeBizBean> getAllCodeBizBean(QueryPage queryPage,CodeBizBean code)
	{
		List<CodeBizBean> codeVoList = new ArrayList<CodeBizBean>();
		List<CodeInfo> codePoList = codeDao.getAllCodeInfo(queryPage,code);
		CodeBizBean codeVo = null;
		for (CodeInfo codePo : codePoList) {
			codeVo = new CodeBizBean();
			try {
				PropertyUtils.copyProperties(codeVo, codePo);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ApplicationException("从po到vo，属性拷贝错误！", e);
			}
			codeVoList.add(codeVo);
		}
		return codeVoList;
	}
	
	

	@Override
	public List<String> getAllCodeName() {
		
		try {
			List<String> strList = codeDao.getAllCodeName();
			return strList;
		} catch (Exception e) {
			//throw new ApplicationException(e);
		}
		return null;
	}

	/**
	 * 新增Code
	 * 
	 * @param vo
	 */
	public void saveCode(CodeBizBean vo) {
		CodeInfo po = new CodeInfo();
		try {
			PropertyUtils.copyProperties(po, vo);
		} catch (Exception e) {
			throw new ApplicationException(e);
		}
		codeDao.save(po);
	}

	/**
	 * 修改Code
	 * 
	 * @param vo
	 */
	public void updateCode(CodeBizBean vo) {
		CodeInfo po = codeDao.getEntityById(vo.getCodeId());
		try {
			PropertyUtils.copyNotNullProperties(po, vo);
		} catch (Exception e) {
			throw new ApplicationException(e);
		}
		codeDao.update(po);
	}
	
	public boolean isExistsCodeNameOrCodeKey(String codeName,String codeKey,Integer codeType)
	{
		return codeDao.isExistsCodeNameOrCodeKey(codeName, codeKey, codeType);
	}
	
	public CodeBizBean getCodeById(Integer codeId)
	{
		CodeInfo po = codeDao.getCodeInfoById(codeId);
		CodeBizBean vo = new CodeBizBean();
		try {
			PropertyUtils.copyProperties(vo, po);
		} catch (Exception e) {
			throw new ApplicationException(e);
		}
		return vo;
	}
	
	/**
	 * 根据Id逻辑删除Code
	 * @param codeId
	 * @return
	 */
	public int tombstoneCode(Integer codeId)
	{
		return codeDao.tombstoneCode(codeId);
	}
	
	/**
	 * Code排序
	 * @param codeId
	 * @param orderBy
	 * @return
	 */
	public int updateCodeOrderBy(Integer codeId,Integer orderBy)
	{
		return codeDao.updateCodeOrderBy(codeId, orderBy);
	}

	 

	@Override
	public String getCodeNameByKey(String code, String key) {
		
		return codeDao.getCodeNameByKey(code,key);
	}

	@Override
	public List<String> findAllCode() {
		return codeDao.findAllCode();
	}

	/**
	 * 根据codeName和codeKey获取code对象
	 * @param codeName	code名称
	 * @param codeKey	code键值
	 * @return
	 */
	@Override
	public CodeBizBean getCodeInfoByCodeNameAndKey(String codeName,
			String codeKey) {
		CodeInfo po = codeDao.getCodeInfoByCodeNameAndKey(codeName, codeKey);
		CodeBizBean vo = new CodeBizBean();
		try {
			PropertyUtils.copyProperties(vo, po);
		} catch (Exception e) {
			throw new ApplicationException(e);
		}
		return vo;
	}

}
